<?php
/**                                          
 * 评论
 * @author Jepin
 * @version 1.0
 * create Date 2014-07-10
 */
class CommentsAction extends CommonAction{
	/**
	 * get session id
	 */
	public function getuserid(){
		dump($_SESSION);
		 echo $_SESSION['mid'];
	  dump($_COOKIE);
		 
		 //echo "100000";
	}
	
	
	public function test(){
		$ch = curl_init();
        $str =URL('comments/Comments/getuserid');
        curl_setopt($ch, CURLOPT_URL, $str);
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
        $output = curl_exec($ch);
        echo $output;
        echo $str;
         var_dump( $output );
	}
    /**
     * 评论显示初始页
     * @param Id 从url地址获取的 文章id
     * 
     */  
    public  function comments(){
    	$article_id = intval($_GET['article_id']);  
    	$count = $this->getCommentsCount($article_id);  // 获取评论数  
    	$this->assign('comments_count',$count);        	      //assign 评论数
    	if(empty($article_id)){
    		$this->error("没有传入文章Id 出现错误  !!");
    	}    	
    	$this->assign("article_id",$article_id);
    	$result = model('Comments')->loadComments($article_id,0); //取得评论内容  开始从0开始
    	$this->assign('comment_list',$result);
    	if(!empty($_GET['type'])){
    		if($_GET['type']==2){
    			$this->display("showComments.html");
    		}
    	}
    	$article_title=M('cms_article')->where('id ='.$article_id)->getField('title') ;
        $this->assign("article_title",$article_title);
    	$this->display();   	 	
    }
    /**
     * 内容页显示的评论信息
     */
    public function showComments(){
    	$article_id = intval($_GET['article_id']);
    	if(!empty($_GET['refer'])){
    		$this->assign("refer",$_GET['refer']);
    	}
    	$count = $this->getCommentsCount($article_id);  // 获取评论数
    	$this->assign('comments_count',$count);        	      //assign 评论数
    	if(empty($article_id)){
    		$this->error("没有传入文章Id 出现错误  !!");
    	}
    	$this->assign("article_id",$article_id);
    	$result = model('Comments')->loadComments($article_id,0); //取得评论内容  开始从0开始
    	$this->assign('comment_list',$result);
    	$this->display();
    }
    /**
     *   新增评论
     */
     public function  addComment(){	 
        if(empty($this->uid) ){                       //判断用户是否登录
            //$this->error("对不起您还未登录 ，不能发表评论");
            exit();
        }
        $data['article_id']  =  intval($_GET['article_id']);
        $data['author']      =  $this->user['uname']; //发表评论的人
        $data['author_ip']   =  get_client_ip();      // 获取IP地址
        $data['commenttime'] =  time();               // 获取系统当前时间
        $data['parent']      =  0;
        $data['user_id']     =  $this->uid;  
        $data['content']     =  common("Html")->text($_POST['comment_content']);//
        $data['type']        =  0 ;                   //0表示为评论  1 表示为回复
        $data['praise_count']=  0;
        $id = model('Comments')->addcomment($data);
        $data['parent']      =  $id;                 //评论的父Id为自己 适用与分组排序
        $data['id']          =  $id;
        
        //重新生成内容页
        file_get_contents(URL('cms/Article/oper',array('ids'=>$data['article_id'],'type'=>'publish')));
        
        $this->assign('v',$data);
        $this->display();
    }
	 
	/**
	 *   回复评论 
	 */
	 public function  replyComment(){
	 	$toID                =  intval($_POST['id']);
	 	$data['article_id']  =  intval($_GET['article_id'] );   //评论对应的文章ID
	    $data['author']      =  $this->user['uname'];   //发表评论的人
	 	$data['author_ip']   =  get_client_ip();        // 获取IP地址
	 	$data['commenttime'] =  time();                 // 获取系统当前时间
	 	$data['user_id']     =  $this->uid;
	 	$data['content']     =  common("Html")->text($_POST['comment_content']);//
	 	$data['type']        =  1 ;                     //0表示为评论  1 表示为回复
	 	$data['parent']      =  intval($_POST['parent']);
	 	$data['praise_count']=  0;
	 	$id = model('Comments')->replyComment($data,$toID);
	 	$data['id']          =  $id;
                
            //重新生成内容页
            file_get_contents(URL('cms/Article/oper',array('ids'=>$data['article_id'],'type'=>'publish')));
 	    
            $this->assign('v',$data);
	    $this->display();
	 }
	 
	 /**
	  * 获取评论数量
	  * @param  $article_id  文章ID
	  */
	 function getCommentsCount($article_id){
	 	$count = model('Comments')->getCommentCount($article_id);
	 	return  $count;
	 }
	 
	 /**
	  * 点击加载更多
	  */
	 public function loadMoreComments(){	 	
	    $page= intval($_GET['page']);
	    $article_id = intval($_GET['article_id']);
	 	$result = model('Comments')->loadComments($article_id,$page);	 	
	 	$this->assign('comment_list',$result);
	 	$this->display();	
	 }
	
    /**
     * 处理登录
     */
    public function  doLogin(){    	 
    	$email    = trim($_GET['email']);
    	$password = trim($_GET['password']);
        $user = model('User')->where("email='".$email."' or phone='".$email."'")->select();
		if (count($user)==0){
			$this->error(L('loginAccountExist'));
		}
		$uid=$user[0]['uid'];
		if(isset($_REQUEST['type'])) {
			if($_REQUEST['type'] == 'bind') {				
				if ($password != $user[0]['password']){
					$this->error(L('accountOrPassword'));
					model('User')->userSessionLogout();
				}
			}else if($_REQUEST['type'] == 'outer') {
				if (!empty($user[0]['salt'])) {
					if (md5(md5($password).$user[0]['salt']) != $user[0]['password']){
						$this->error(L('accountOrPassword'));
						model('User')->userSessionLogout();
					}
				}else{
					if (md5($password) != $user[0]['password']){			
						$this->error(L('accountOrPassword'));
						model('User')->userSessionLogout();
					}
				}
			}
		} else {
			if (!empty($user[0]['salt'])) {
				if (md5(md5($password).$user[0]['salt']) != $user[0]['password']){
					$this->error(L('accountOrPassword'));
					model('User')->userSessionLogout();
				}
			}else{
				if (md5($password) != $user[0]['password']){			
					$this->error(L('accountOrPassword'));
					model('User')->userSessionLogout();
				}
			}
		}
		if ($user[0]['is_active']==0){
			$this->error(L('thisUserActivated')); 
		}
		//检查禁止登录的用户UID
		$audit = model('Data')->lget('audit');
		if($audit['banuid']==1){
			$bannedUids = $audit['banneduids'];
			if(!empty($bannedUids)){
				$bannedUids = explode('|',$bannedUids);
				if(in_array($uid,$bannedUids)){
					$this->error(L('thisUserProhibition'));
					model('User')->userSessionLogout();
				}
			}
		}
		$data['uid'] = $uid;
		$data['username'] = $user[0]['uname'];
		$data['email'] = $user[0]['email'];
		$data['password'] = $password;
		//common('Url')->getSysOperUrl('synlogin',$data);
		//记录session
		$_SESSION['mid']			= $uid;
		$_SESSION['uname']			= $user[0]['uname'];
		if ($user[0]['admin_level']=='1') {
			$_SESSION['administrator'] = true;
			$_SESSION ['loginUserName'] = $user[0]['uname'];
			if(in_array($uid,C('NOT_AUTH_ADMIN'))){
				$_SESSION[C('ADMIN_AUTH_KEY')]=$uid;
			}
		} else
			unset($_SESSION['administrator']);
		//记录绑定信息
		D('Login')->loginBindInfo();
		//记录登录信息
		model('User')->recordLogin($uid);
		$uname=$user[0]['uname'];
		if($uname == null || $uname ==="") {
			$this->error(L('nickname_is_null'),URL('home/Public/step1'));
			exit();
		}
		// 登录积分
		model('Operate')->setUserCredit($uid, 'user_login');
		if($_POST['remember'] == "1") {
			setcookie("ylsnsemail", $user[0]['email'], time()+3600*24*365,'/',C('COOKIE_DOMAIN'));
			if (!empty($user[0]['salt'])) {
				$pass = md5(md5($password).$user[0]['salt']);
			}else{
				$pass = md5($password);
			}
			setcookie("ylsnspassword", $pass, time()+3600*24*365,'/',C('COOKIE_DOMAIN'));
		}
                setcookie("i8username", $user[0]['email'], time()+3600*24*365,'/',C('COOKIE_DOMAIN'));
                setcookie("i8userid", $user[0]['uid'], time()+3600*24*365,'/',C('COOKIE_DOMAIN'));
		$userurl = URL('home/Space/index', array ('uid' => $uid ));
		$userpic = 'http://'.SITE_DOMAIN.common("Url") -> getUserFaceUrl($uid,"a"); 
		//此处将登录用户的 姓名  跳转到sns、 连接、用户对应的图片地址   传入到页面  为点击回复按钮是 显示用户信息提供数据
		$html['username']= $user[0]['uname'];  //登录用户名
		$html['userurl'] = $userurl;           //用户对应的sns 地址
		$html['userpic'] = $userpic;           //用户对应的图片地址
		$this->ajaxReturn($html,"登录成功",1);
    } 
    
    /**
     * 点赞  功能
     */
   Public function pointPraise(){
   	   $id =   intval($_GET['id']);                          //评论ID
   	   $type = intval($_GET['type']);                      //1 表示赞+1  0表示赞-1
   	   if(empty($id)){
   	    	echo false;
   	    	exit();
   	   }
   	   $result = model('Comments')->pointPraise($id,$type); 
   	   if($result=="true"){
   	    	echo "true";
   	   }else {
   	     	echo "false";
   	   }
   }
   
   /**
    * 删除评论
    * @param id 评论ID
    * @param uid 用户登录Id
    */
   public function delComment(){
   	  $id     = intval($_GET['id']);     //评论Id
   	  $uid    = $this->uid;      //登录用户ID
   	  if(empty($id)){
   	  	$this->ajaxReturn(0,'您的操作有误，请确认你的操作是否正确！！',0);
   	  }
   	  if(empty($uid)){
   	  	$this->ajaxReturn(0,'对不起您还未登录，不能做此项操作！',0);
   	  }
   	   $res=model('Comments')->delComments($uid,$id);
   	   if($res){
   	   	echo "true";
   	   }else{
   	    	$this->ajaxReturn(0,'您的操作有误，请确认你的操作是否正确！！',0);
   	   }
   	  exit(); 
   } 
   
}
?>